<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Class: Synthesize</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Class</strong></td>
          <td class="class-name-in-header">Synthesize</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../files/lib/synthesize_rb.html">
                lib/synthesize.rb
                </a>
        <br />
            </td>
        </tr>

        <tr class="top-aligned-row">
            <td><strong>Parent:</strong></td>
            <td>
                Object
            </td>
        </tr>
        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
Synthesis Generate Basic Waveforms and Noise
</p>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000001">new</a>&nbsp;&nbsp;
      <a href="#M000006">noise</a>&nbsp;&nbsp;
      <a href="#M000004">sawtooth</a>&nbsp;&nbsp;
      <a href="#M000007">silence</a>&nbsp;&nbsp;
      <a href="#M000002">sin</a>&nbsp;&nbsp;
      <a href="#M000003">square</a>&nbsp;&nbsp;
      <a href="#M000008">to_wav</a>&nbsp;&nbsp;
      <a href="#M000005">triangle</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





    <div id="attribute-list">
      <h3 class="section-bar">Attributes</h3>

      <div class="name-list">
        <table>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">amplitude</td>
          <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">bits_per_sample</td>
          <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">channels</td>
          <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">duration</td>
          <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">frequency</td>
          <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">sample_rate</td>
          <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        <tr class="top-aligned-row context-row">
          <td class="context-item-name">waveform</td>
          <td class="context-item-value">&nbsp;[RW]&nbsp;</td>
          <td class="context-item-desc"></td>
        </tr>
        </table>
      </div>
    </div>
      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Class methods</h3>

      <div id="method-M000001" class="method-detail">
        <a name="M000001"></a>

        <div class="method-heading">
          <a href="#M000001" class="method-signature">
          <span class="method-name">new</span><span class="method-args">(frequency, amplitude, duration)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000001-source">
<pre>
    <span class="ruby-comment cmt"># File lib/synthesize.rb, line 8</span>
 8:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>(<span class="ruby-identifier">frequency</span>, <span class="ruby-identifier">amplitude</span>, <span class="ruby-identifier">duration</span>)
 9:     <span class="ruby-ivar">@frequency</span> = <span class="ruby-identifier">frequency</span>
10:     <span class="ruby-ivar">@amplitude</span> = <span class="ruby-identifier">amplitude</span>
11:     <span class="ruby-ivar">@duration</span> = <span class="ruby-identifier">duration</span>
12:     <span class="ruby-ivar">@channels</span> = <span class="ruby-value">2</span>
13:     <span class="ruby-ivar">@sample_rate</span> = <span class="ruby-value">44100</span>
14:     <span class="ruby-ivar">@bits_per_sample</span> = <span class="ruby-value">16</span>
15:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000006" class="method-detail">
        <a name="M000006"></a>

        <div class="method-heading">
          <a href="#M000006" class="method-signature">
          <span class="method-name">noise</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000006-source">
<pre>
    <span class="ruby-comment cmt"># File lib/synthesize.rb, line 81</span>
81:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">noise</span>
82:     <span class="ruby-ivar">@waveform</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@duration</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@sample_rate</span>)
83:     <span class="ruby-ivar">@waveform</span>.<span class="ruby-identifier">each_index</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
84:       <span class="ruby-identifier">waveform</span>[<span class="ruby-identifier">i</span>] = <span class="ruby-identifier">rand</span>(<span class="ruby-value">2</span>) <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
85:     <span class="ruby-keyword kw">end</span>
86:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000004" class="method-detail">
        <a name="M000004"></a>

        <div class="method-heading">
          <a href="#M000004" class="method-signature">
          <span class="method-name">sawtooth</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000004-source">
<pre>
    <span class="ruby-comment cmt"># File lib/synthesize.rb, line 49</span>
49:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">sawtooth</span>
50:     <span class="ruby-identifier">step</span> = (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@frequency</span>).<span class="ruby-identifier">fdiv</span>(<span class="ruby-ivar">@sample_rate</span>)
51:     <span class="ruby-identifier">phase</span> = <span class="ruby-value">0</span>
52:     <span class="ruby-ivar">@waveform</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@duration</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@sample_rate</span>)
53:     <span class="ruby-ivar">@waveform</span>.<span class="ruby-identifier">each_index</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
54:       <span class="ruby-ivar">@waveform</span>[<span class="ruby-identifier">i</span>] = <span class="ruby-ivar">@amplitude</span> <span class="ruby-operator">-</span> (<span class="ruby-ivar">@amplitude</span>.<span class="ruby-identifier">fdiv</span>(<span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>)) <span class="ruby-operator">*</span> <span class="ruby-identifier">phase</span>
55:       
56:       <span class="ruby-identifier">phase</span> = <span class="ruby-identifier">phase</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">step</span>
57:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">phase</span> <span class="ruby-operator">&gt;</span> (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>)
58:         <span class="ruby-identifier">phase</span> = <span class="ruby-identifier">phase</span> <span class="ruby-operator">-</span> (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>)
59:       <span class="ruby-keyword kw">end</span>
60:     <span class="ruby-keyword kw">end</span>
61:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000007" class="method-detail">
        <a name="M000007"></a>

        <div class="method-heading">
          <a href="#M000007" class="method-signature">
          <span class="method-name">silence</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000007-source">
<pre>
    <span class="ruby-comment cmt"># File lib/synthesize.rb, line 88</span>
88:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">silence</span>
89:     <span class="ruby-ivar">@waveform</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@duration</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@sample_rate</span>)
90:     <span class="ruby-ivar">@waveform</span>.<span class="ruby-identifier">each_index</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
91:       <span class="ruby-identifier">waveform</span>[<span class="ruby-identifier">i</span>] = <span class="ruby-value">0</span>
92:     <span class="ruby-keyword kw">end</span>
93:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000002" class="method-detail">
        <a name="M000002"></a>

        <div class="method-heading">
          <a href="#M000002" class="method-signature">
          <span class="method-name">sin</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000002-source">
<pre>
    <span class="ruby-comment cmt"># File lib/synthesize.rb, line 17</span>
17:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">sin</span>
18:     <span class="ruby-identifier">step</span> = (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@frequency</span>).<span class="ruby-identifier">fdiv</span>(<span class="ruby-ivar">@sample_rate</span>)
19:     <span class="ruby-identifier">phase</span> = <span class="ruby-value">0</span>
20:     <span class="ruby-ivar">@waveform</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@duration</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@sample_rate</span>)
21:     <span class="ruby-ivar">@waveform</span>.<span class="ruby-identifier">each_index</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
22:       <span class="ruby-ivar">@waveform</span>[<span class="ruby-identifier">i</span>] = <span class="ruby-ivar">@amplitude</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span>.<span class="ruby-identifier">sin</span>(<span class="ruby-identifier">phase</span>)
23:       
24:       <span class="ruby-identifier">phase</span> = <span class="ruby-identifier">phase</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">step</span>
25:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">phase</span> <span class="ruby-operator">&gt;</span> (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>)
26:         <span class="ruby-identifier">phase</span> = <span class="ruby-identifier">phase</span> <span class="ruby-operator">-</span> (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>)
27:       <span class="ruby-keyword kw">end</span>
28:     <span class="ruby-keyword kw">end</span>
29:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000003" class="method-detail">
        <a name="M000003"></a>

        <div class="method-heading">
          <a href="#M000003" class="method-signature">
          <span class="method-name">square</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000003-source">
<pre>
    <span class="ruby-comment cmt"># File lib/synthesize.rb, line 31</span>
31:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">square</span>
32:     <span class="ruby-identifier">step</span> = (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@frequency</span>).<span class="ruby-identifier">fdiv</span>(<span class="ruby-ivar">@sample_rate</span>)
33:     <span class="ruby-identifier">phase</span> = <span class="ruby-value">0</span>
34:     <span class="ruby-ivar">@waveform</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@duration</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@sample_rate</span>)
35:     <span class="ruby-ivar">@waveform</span>.<span class="ruby-identifier">each_index</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
36:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">phase</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>
37:         <span class="ruby-ivar">@waveform</span>[<span class="ruby-identifier">i</span>] = <span class="ruby-ivar">@amplitude</span>
38:       <span class="ruby-keyword kw">else</span>
39:         <span class="ruby-ivar">@waveform</span>[<span class="ruby-identifier">i</span>] = <span class="ruby-operator">-</span><span class="ruby-ivar">@amplitude</span>
40:       <span class="ruby-keyword kw">end</span>
41:       
42:       <span class="ruby-identifier">phase</span> = <span class="ruby-identifier">phase</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">step</span>
43:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">phase</span> <span class="ruby-operator">&gt;</span> (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>)
44:         <span class="ruby-identifier">phase</span> = <span class="ruby-identifier">phase</span> <span class="ruby-operator">-</span> (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>)
45:       <span class="ruby-keyword kw">end</span>
46:     <span class="ruby-keyword kw">end</span>
47:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000008" class="method-detail">
        <a name="M000008"></a>

        <div class="method-heading">
          <a href="#M000008" class="method-signature">
          <span class="method-name">to_wav</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000008-source">
<pre>
     <span class="ruby-comment cmt"># File lib/synthesize.rb, line 95</span>
 95:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">to_wav</span>
 96:     <span class="ruby-identifier">waveform</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>
 97:     
 98:     <span class="ruby-ivar">@waveform</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">w</span><span class="ruby-operator">|</span>
 99:       <span class="ruby-identifier">value</span> = <span class="ruby-identifier">w</span> <span class="ruby-operator">*</span> ((<span class="ruby-value">2</span><span class="ruby-operator">**</span><span class="ruby-ivar">@bits_per_sample</span>) <span class="ruby-operator">/</span> <span class="ruby-value">2</span>)
100:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
101:         <span class="ruby-identifier">value</span> = <span class="ruby-identifier">value</span> <span class="ruby-operator">-</span> <span class="ruby-value">1</span>
102:       <span class="ruby-keyword kw">elsif</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0</span>
103:         <span class="ruby-identifier">value</span> = <span class="ruby-identifier">value</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>
104:       <span class="ruby-keyword kw">end</span>
105:       <span class="ruby-ivar">@channels</span>.<span class="ruby-identifier">times</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span>
106:        <span class="ruby-identifier">waveform</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">value</span>)
107:      <span class="ruby-keyword kw">end</span>
108:     <span class="ruby-keyword kw">end</span>
109:     
110:     <span class="ruby-identifier">block_align</span> = (<span class="ruby-ivar">@bits_per_sample</span> <span class="ruby-operator">/</span> <span class="ruby-value">8</span>) <span class="ruby-operator">*</span> <span class="ruby-ivar">@channels</span>
111:     <span class="ruby-identifier">bytes_per_second</span> = <span class="ruby-identifier">block_align</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@sample_rate</span>
112:     
113:     <span class="ruby-identifier">header</span> = <span class="ruby-constant">Array</span>[
114:       <span class="ruby-value str">&quot;RIFF&quot;</span>,                     <span class="ruby-comment cmt">#Chunk ID</span>
115:         <span class="ruby-identifier">bytes_per_second</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@duration</span> <span class="ruby-operator">+</span> <span class="ruby-value">44</span> <span class="ruby-operator">-</span> <span class="ruby-value">8</span>,  <span class="ruby-comment cmt">#Chunk Data Size</span>
116:         <span class="ruby-value str">&quot;WAVE&quot;</span>,                     <span class="ruby-comment cmt">#RIFF Type</span>
117:       <span class="ruby-value str">&quot;fmt &quot;</span>,                     <span class="ruby-comment cmt">#Chunk ID</span>
118:         <span class="ruby-value">16</span>,                         <span class="ruby-comment cmt">#Chunk Data Size</span>
119:         <span class="ruby-value">1</span>,                          <span class="ruby-comment cmt">#Compression Code</span>
120:         <span class="ruby-ivar">@channels</span>,                  <span class="ruby-comment cmt">#Number of Channels</span>
121:         <span class="ruby-ivar">@sample_rate</span>,               <span class="ruby-comment cmt">#Sample Rate</span>
122:         <span class="ruby-identifier">bytes_per_second</span>,           <span class="ruby-comment cmt">#Bytes per Second</span>
123:         <span class="ruby-identifier">block_align</span>,                <span class="ruby-comment cmt">#Block Align</span>
124:         <span class="ruby-ivar">@bits_per_sample</span>,           <span class="ruby-comment cmt">#Significant Bits per Sample</span>
125:       <span class="ruby-value str">&quot;data&quot;</span>,                     <span class="ruby-comment cmt">#Chunk ID</span>
126:         <span class="ruby-identifier">bytes_per_second</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@duration</span>] <span class="ruby-comment cmt">#Chunk Data Size</span>
127:       
128:         
129:     <span class="ruby-identifier">header_data</span> = <span class="ruby-identifier">header</span>.<span class="ruby-identifier">pack</span>(<span class="ruby-value str">&quot;A4VA4A4VvvVVvvA4V&quot;</span>)
130:     <span class="ruby-identifier">wav_data</span> = <span class="ruby-identifier">waveform</span>.<span class="ruby-identifier">pack</span>(<span class="ruby-value str">&quot;s*&quot;</span>)
131:     
132:     <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">header_data</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">wav_data</span>
133:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000005" class="method-detail">
        <a name="M000005"></a>

        <div class="method-heading">
          <a href="#M000005" class="method-signature">
          <span class="method-name">triangle</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000005-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000005-source">
<pre>
    <span class="ruby-comment cmt"># File lib/synthesize.rb, line 63</span>
63:   <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">triangle</span>
64:     <span class="ruby-identifier">step</span> = (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@frequency</span>).<span class="ruby-identifier">fdiv</span>(<span class="ruby-ivar">@sample_rate</span>)
65:     <span class="ruby-identifier">phase</span> = <span class="ruby-value">0</span>
66:     <span class="ruby-ivar">@waveform</span> = <span class="ruby-constant">Array</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@duration</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@sample_rate</span>)
67:     <span class="ruby-ivar">@waveform</span>.<span class="ruby-identifier">each_index</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span>
68:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">phase</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>
69:         <span class="ruby-ivar">@waveform</span>[<span class="ruby-identifier">i</span>] = <span class="ruby-operator">-</span><span class="ruby-ivar">@amplitude</span> <span class="ruby-operator">+</span> (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@amplitude</span>.<span class="ruby-identifier">fdiv</span>(<span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>)) <span class="ruby-operator">*</span> <span class="ruby-identifier">phase</span>
70:       <span class="ruby-keyword kw">else</span>
71:         <span class="ruby-ivar">@waveform</span>[<span class="ruby-identifier">i</span>] = <span class="ruby-value">3</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@amplitude</span> <span class="ruby-operator">-</span> (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-ivar">@amplitude</span>.<span class="ruby-identifier">fdiv</span>(<span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>)) <span class="ruby-operator">*</span> <span class="ruby-identifier">phase</span>
72:       <span class="ruby-keyword kw">end</span>
73:       
74:       <span class="ruby-identifier">phase</span> = <span class="ruby-identifier">phase</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">step</span>
75:       <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">phase</span> <span class="ruby-operator">&gt;</span> (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>)
76:         <span class="ruby-identifier">phase</span> = <span class="ruby-identifier">phase</span> <span class="ruby-operator">-</span> (<span class="ruby-value">2</span> <span class="ruby-operator">*</span> <span class="ruby-constant">Math</span><span class="ruby-operator">::</span><span class="ruby-constant">PI</span>)
77:       <span class="ruby-keyword kw">end</span>
78:     <span class="ruby-keyword kw">end</span>
79:   <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>